package com.todo.strategy.online.application.handler.statusChange;

import com.alibaba.fastjson.JSONObject;
import com.todo.strategy.common.enums.StrategyStatusEnum;
import com.todo.strategy.online.application.entity.bo.StrategyInfoBO;
import com.todo.strategy.online.application.entity.context.StrategyStatusContext;
import com.todo.strategy.online.application.service.StrategyInfoService;
import com.todo.strategy.online.infrastructure.helper.es.entity.BasicStrategyEntity;
import com.todo.strategy.online.infrastructure.helper.es.mapper.BasicStrategyEntityMapper;
import lombok.extern.slf4j.Slf4j;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2024/12/12 19:37
 * @description
 */
@Slf4j
@Component
public class DisableHandler extends AbstractStrategyStatusHandler {

    @Resource
    private StrategyInfoService strategyInfoService;
    @Resource
    private BasicStrategyEntityMapper strategyEntityMapper;


    @Override
    protected Boolean doStatusHandle(StrategyStatusContext context) {
        Long strategyId = context.getStrategyId();
        StrategyInfoBO strategyInfoBO = strategyInfoService.queryStrategyInfoById(strategyId);
        if (strategyInfoBO == null) {
            log.error("DisableHandler#doStatusHandle 用户攻略发布状态处理完成，数据为空，默认执行成功 context:{}", JSONObject.toJSONString(context));
            return true;
        }
        if (!strategyInfoBO.getStatus().equals(context.getCurrentStatus())) {
            log.error("DisableHandler#doStatusHandle 用户攻略当前数据状态与发送消息状态不一致，无法处理当前状态下的数据情况 context:{}，当前状态 :{}", JSONObject.toJSONString(context), strategyInfoBO.getStatus());
            return true;
        }
        LambdaEsQueryWrapper<BasicStrategyEntity> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(BasicStrategyEntity::getMysqlId, strategyId);
        strategyEntityMapper.delete(wrapper);
        return true;
    }

    @Override
    protected boolean match(StrategyStatusContext context) {
        if (context == null || context.getStrategyId() == null || context.getCurrentStatus() == null) {
            log.error("DisableHandler#match 用户攻略前置数据匹配失败，数据缺失或不存在 context:{}", JSONObject.toJSONString(context));
            return false;
        }
        return true;
    }

    @Override
    protected StrategyStatusEnum doGetDealStatus() {
        return StrategyStatusEnum.DISABLE;
    }
}
